# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>

_realname=icu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=77.1
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="International Components for Unicode library (mingw-w64)"
url="https://icu.unicode.org/home/"
msys2_repository_url="https://github.com/unicode-org/icu"
msys2_references=(
  "cpe: cpe:/a:icu-project:international_components_for_unicode"
)
license=('spdx:ICU')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "autoconf-archive")
source=(https://github.com/unicode-org/icu/releases/download/release-${pkgver//./-}/icu4c-${pkgver//./_}-src.tgz
        https://sources.debian.org/data/main/i/icu/76.1-3/debian/patches/reverse_of_ICU-22610.patch
        0011-sbin-dir.mingw.patch
        0012-libprefix.mingw.patch
        0014-mingwize-pkgdata.mingw.patch
        0015-debug.mingw.patch
        0016-icu-pkgconfig.patch
        0021-mingw-static-libraries-without-s.patch
        0023-fix-twice-include-platform_make_fragment.patch)
sha256sums=('588e431f77327c39031ffbb8843c0e3bc122c211374485fa87dc5f3faff24061'
            'c515e2b37e230895080d36207dfe078eb638f974220baa487ede2593e820944c'
            '4f4787caeccf70607cf0cbde0c005f05f5c6de1543265a927839122405b4054f'
            'e7ecdafe85e18a4a4b5f29bbfde38776521a848e5b65089a2379b90e59f1592d'
            'd9f5f756443d7d14175aca06f4f1bb33237b58907e5db7a6ff7e38d2c4ea7957'
            '0181f9ff8b7dd0a423869d8772da0a4feea64188347eb0dee7eb059aea92131c'
            '87ebe8962f8c387714f2a697a664a0c49aed2331b988548069d0c211abc36e05'
            'd8612f40b1731d9a94290afcf80c896184a2f15b8ae8f23b3643c64f6cabfa2f'
            '12aa647cff22a721bdbceb4e77f5e66c6616e0e050dca8172acfd3365553fff4')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/icu"

  apply_patch_with_msg \
    reverse_of_ICU-22610.patch \
    0011-sbin-dir.mingw.patch \
    0012-libprefix.mingw.patch \
    0014-mingwize-pkgdata.mingw.patch \
    0015-debug.mingw.patch \
    0016-icu-pkgconfig.patch \
    0021-mingw-static-libraries-without-s.patch \
    0023-fix-twice-include-platform_make_fragment.patch

  cd source
  autoreconf -vfi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--enable-release")
  else
    _extra_config+=("--enable-debug")
  fi

  ../icu/source/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-rpath \
    --enable-shared \
    --enable-static \
    --with-data-packaging=dll \
    --disable-tests \
    --disable-samples \
    "${_extra_config[@]}"

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  PATH="${srcdir}"/icu/build-${MSYSTEM}/bin:${srcdir}/icu/build-${MSYSTEM}/lib:$PATH \
  make V=1 -k check || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make install DESTDIR="${pkgdir}" RM="rm -rf"

  mv "${pkgdir}${MINGW_PREFIX}"/bin/*.a "${pkgdir}${MINGW_PREFIX}"/lib/
}
